package com.aps.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import com.aps.entity.ApsExeption;
import com.aps.entity.domain.dish.Dish;
import com.aps.entity.domain.dish.DishProcess;
import com.aps.entity.eunm.ResultType;
import com.aps.page.BaseQuery;
import com.aps.page.Pagination;
import com.aps.service.IDishProcessService;
import com.aps.service.rep.DishProcessRep;
import com.aps.util.JdbcQueryUtil;

/**
 * @author liaoguangan
 * @description <>
 * @date 2018/4/3 16:32
 */

@Service
public class DishProcessServiceImpl implements IDishProcessService {

    @Autowired
    private DishProcessRep dishProcessRep;

    @Autowired
    private JdbcTemplate jdbcTemplate;

    /**
     * 根据加工工序更新菜品加工时间
     * @param dish
     * @param dspReq
     * @param dishNum
     */
    @Override
    public void updateDishProcessTime(Dish dish, String dspReq, String dishNum) {
        //查询加工工序
        DishProcess process = dishProcessRep.findByCodeAndDishCode(dspReq, dishNum);
        if (null != process && null != process.getProcessTime()
                && null != process.getProcessTimeUnit()) {
            dish.setProcessTime(process.getProcessTime());
            dish.setProcessTimeUnit(process.getProcessTimeUnit());
        }
    }

    /**
     * 查询列表
     *
     * @param baseQuery
     * @return
     */
    @Override
    public Pagination<DishProcess> list(BaseQuery baseQuery) {
        String sql = " select adp.*, ad.name dish_name from aps_dish_process adp "
                + " left join aps_dish ad on adp.dish_code = ad.code ";
        return new Pagination<DishProcess>().search(jdbcTemplate, DishProcess.class, sql,
                new Object[] {}, baseQuery, "adp");
    }

    /**
     * 增加
     *
     * @param dishProcess
     * @return
     */
    @Override
    public DishProcess add(DishProcess dishProcess) {
        DishProcess dp = dishProcessRep.findByCode(dishProcess.getCode());
        if (null != dp) {
            throw new ApsExeption(ResultType.RECORD_ALWAYS_EXIST);
        }
        return dishProcessRep.save(dishProcess);
    }

    /**
     * 修改
     *
     * @param dishProcess
     */
    @Override
    public DishProcess update(DishProcess dishProcess) {
        DishProcess dp = dishProcessRep.findByCode(dishProcess.getCode());
        if (null != dp && !dp.getId().equals(dishProcess.getId())) {
            throw new ApsExeption(ResultType.RECORD_ALWAYS_EXIST);
        }

        return dishProcessRep.save(dishProcess);
    }

    /**
     * 删除
     *
     * @param id
     */
    @Override
    public void remove(String id) {
        DishProcess dp = dishProcessRep.findOne(id);
        if (null == dp) {
            throw new ApsExeption(ResultType.RECORD_NOT_EXIST);
        }

        dp.setIsDeleted(true);
        dishProcessRep.save(dp);
    }

    /**
     * 查询
     *
     * @param id
     */
    @Override
    public DishProcess get(String id) {
        String sql = " select adp.*, ad.name dish_name from aps_dish_process adp "
                + " left join aps_dish ad on adp.dish_code = ad.code "
                + " where adp.is_deleted = false and adp.id = ?";

        return new JdbcQueryUtil<>(DishProcess.class).query(jdbcTemplate, sql, new Object[] { id });
    }
}
